define(function(require, exports){
	var pubjs	= require('pubjs');
	var view	= require('@base/view');
	var util	= require('util');

	var Base = view.container.extend({
		init:function(config){
			config = pubjs.conf(config, {
				'tplFile':	'tpl/admin/userAccess/editChannel.tpl',
				'view_model': {
					'headTitle': '渠道授权'
					,'isEdit':false
					,'isDefault': false
					,'name': ''
					,'_isDefaultRadio': 1
					,'jumpUrl': 'admin/userAccess'
				},
				urlAdd: 'userAccess/updateAdx',
				urlInfo: 'userAccess/adxInfo'
			});

			this.$id = null;
			this.Super('init', arguments);
		}
		,afterBuild:function(){
			var self = this;
			this.$jumpUrl = this.getConfig('jumpUrl');
			self.$channels = pubjs.config('exchange');
			self.$defaultChannel = this.$channels && this.$channels[0].id;

			var config = {
				// 表单取消按钮
				'cancel': {
					'value': LANG('取消'),
					'class': 'btn btn-link mr20'
				}
				// 表单保存按钮
				,'save': {
					'value': LANG('保存'),
					'class': 'btn btn-confirm'
				}
				,select: {
					'defaultValue': self.$defaultChannel
					,'items': self.$channels
					,'reqType': 'websocket'
					,'prefix': 'userAccessEditchannel'
					,'silent': false
					,'hasSearch': true
					,'hasOperator': true
				}
				,Tip: {
					'width': 300,
					'data': LANG('如选择默认,那就将用户设为默认渠道, 如果授权渠道,就根据用户授权的渠道为依据.')
				}
			};

			self.buildFromTemplate(config);
		}
		,afterBuildTemplate: function() {
			var self = this;
			var vm = self.vm;
			/*
			vm.watch('SourceId', function(v) {
				self.$.Pid.setParam()
			});
			*/
		}
		,load: function(id){
			var self = this;
			self.$id = id;
			if(id){
				self.vm.$.isEdit = true;
				pubjs.loading.show();
				pubjs.mc.send(this.getConfig('urlInfo'), {UserId: id}, function(err, data){
					pubjs.loading.hide();
					if(err){
						pubjs.alert(err.message);
						pubjs.controller.navigate(self.$jumpUrl);
						return false;
					}

					self.setData(data);
				})
			}

			return self;
		}
		,validate: function(data){
			var vm = this.vm.$;
			if(!data.UserId) {
				pubjs.alert(LANG('参数错误,用户不能为空'));
				return false;
			}
			if(!data.isDefaultAdx && !data.AdxIds) {
				pubjs.alert(LANG('不选默认时,渠道选择不能为空.'))
			}

			return true;
		}
		,setData: function(data) {
			this.$data = data;
			var self = this
				,vm = self.vm.$;

			vm._isDefaultRadio = data.isDefaultAdx ? 1: 0;
			vm.name = data.Name;
			self.$.select.setValue(data.OwnAdxIds);
			return self;
		}
		,getData: function(){
			var self = this
				,data
				,vm = self.vm.$;

			data = {
				UserId: self.$id
				,isDefaultAdx: vm._isDefaultRadio == '1'
			};

			if(!data.isDefaultAdx) {
				data.AdxIds = self.$.select.getValue();
			}

			return data;
		}
		,reset: function(){
			this.$id = 0;
			this.vm.reset();
			return this;
		}
		,save: function() {
			var self = this;
			var data = self.getData();
			if(self.validate(data)) {
				pubjs.loading.show();
				pubjs.mc.send(self.getConfig('urlAdd'), data, self.afterSave.bind(self))
			}

			return false;
		}
		,afterSave: function(err, data) {
			pubjs.loading.hide();
			if(err) {
				pubjs.alert(err.message);
				return false;
			}

			pubjs.controller.navigate(this.$jumpUrl);
		}
		,onInputClick: function(ev) {
			if(ev.name == 'save') {
				this.save();
			}
			else if(ev.name == 'cancel') {
				pubjs.controller.navigate(this.$jumpUrl);
			}
		}
	});

	exports.channel = Base;
});